package cn.wolfcode.wolf2w.service.impl;
import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.query.TravelCondition;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.repository.TravelRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.security.auth.login.LoginException;
import java.util.Date;
import java.util.List;
import cn.wolfcode.wolf2w.LogicException;

@Service
//@Transactional mongodb的事务必须是复制集的方式
public class TravelServiceImpl implements ITravelService{
    @Autowired
    private TravelRepository repository;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IDestinationService destinationService;


    @Override
    public void save(Travel travel) {
        travel.setId(null);//把id设置为null
        repository.save(travel);//存入travel对象

    }

    @Override
    public void update(Travel travel) {
        repository.save(travel);//编辑和保存是一样的,更新之后则要保存travel对象

    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);

    }

    @Override
    public Travel get(String id) {
        //orEls,查询id,存在就返回,不承诺在就返回null
        Travel travel = repository.findById(id).orElse(null);

        if (travel != null){
            travel.setAuthor(userInfoService.get(travel.getUserId()));
            travel.setDest(destinationService.get(travel.getDestId()));
        }
        return travel;

    }

    @Override
    public List<Travel> list() {
        //查询所有
        return repository.findAll();
    }

    @Override
    public Page<Travel> query(TravelQuery qo) {

        //分页查询
        //查询对象
        Query query = new Query();
        if (StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
        //出行时间
        TravelCondition day = qo.getDay();
        if (day != null){
            query.addCriteria(Criteria.where("day")
                    .gte(day.getMin()).lte(day.getMax()));
        }
        //人均消费
        TravelCondition ex = qo.perExpendType();
        if (ex != null){
            query.addCriteria(Criteria.where("perExpend")
                    .gte(ex.getMin()).lte(ex.getMax()));
        }
        Pageable pageable = null;
        if (qo.getOrderType() == 1){
            pageable = PageRequest.of(qo.getCurrentPage() - 1,
                    qo.getPageSize(), Sort.Direction.ASC, "createTime");
        }else if (qo.getOrderType() == 2){
             pageable = PageRequest.of(qo.getCurrentPage() - 1,
                    qo.getPageSize(), Sort.Direction.ASC, "viewnum");
        }else {
             pageable = PageRequest.of(qo.getCurrentPage() - 1,
                    qo.getPageSize(), Sort.Direction.ASC, "_id");
        }

        Page<Travel> page = DBHelper.query(template, Travel.class, query, pageable);
        //页面缺失用户对象[维护用户对象]
        for (Travel travel : page) {
            travel.setAuthor(userInfoService.get(travel.getUserId()));
        }
        return page;
    }

    @Override
    public void saveOrUpdate(Travel travel) {

        //目的地名称
        Destination destination = destinationService.get(travel.getDestId());
        travel.setDestName(destination.getName());

        //用户id


        //最后更新时间
        travel.setLastUpdateTime(new Date());
        if (StringUtils.hasLength(travel.getId())){
            //update特殊案例,页面更新的数据,刚好跟集合中字段个数一致,所以这种等价于全量的更新
            this.update(travel);
        }else {
            //save
            //创建时间
            travel.setCreateTime(new Date());
            this.save(travel);
        }

    }

    //思考审核流程

    @Override
    public void audit(String id, int state){

        //判断是否满足审核条件
        Travel travel = this.get(id);
        //审核通过了之后需要做什么
        if (travel == null){
            throw new LogicException("参数异常");
        }
        if(travel.getState() != Travel.STATE_WAITING){
            throw new LogicException("游记不是待审核状态,不允许审核");
        }

        if (state == Travel.STATE_RELEASE){
            //审核通过之后要做什么
            //1.修改状态为审核通过状态
            travel.setState(Travel.STATE_RELEASE);
            //2.修改发布时间
            travel.setReleaseTime(new Date());
            //3.最后更新时间
            travel.setLastUpdateTime(new Date());
            //4.记录审核信息(审核人,审核时间,审核状态,审核备注等等)
            this.update(travel);
        }else if (state == Travel.STATE_REJECT){
            //审核拒绝之后要做什么
            //1.修改状态为审核通过状态
            travel.setState(Travel.STATE_REJECT);
            //2.修改发布时间
            travel.setReleaseTime(new Date());
            //3.最后更新时间
            travel.setLastUpdateTime(new Date());
            //4.记录审核信息(审核人,审核时间,审核状态,审核备注等等)
            this.update(travel);
        }else{
            throw new LogicException("状态不对");
        }
    }

    @Override
    public List<Travel> queryViewnumTop3(String destId) {
        //1.目的地id必须指定
        //2.点击量前三(倒叙)
        //3.截取前面三个
        Pageable pageable = PageRequest.of(0,3,
                Sort.Direction.DESC,"viewnum");

        List<Travel> list = repository.findByDestId(destId,pageable);
        return list;

    }
}
